#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <left4dhooks>

#define PLUGIN_VERSION "1.0"

public Plugin myinfo =
{
	name = "l4d2_tank_witch_yeeter_blaster",
	author = "豆瓣酱な",
	description = "击退特感女巫榴弹枪.",
	version = PLUGIN_VERSION,
	url = "https://gitee.com/sweet-and-sour-bombs/adfvsdsd"
};

public void OnPluginStart()
{

	HookEvent("player_hurt", Event_PlayerHurt);

}


bool IsValidClient(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2 && IsPlayerAlive(client);
}


bool IsValidInfected(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 3 && IsPlayerAlive(client);
}

bool IsValidWitch(int victim)
{
	if (IsValidEntity(victim))
	{
		static char sName[8];
		GetEdictClassname(victim, sName, sizeof sName);
		if (strcmp(sName, "witch") == 0)
			return true;
	}

	return false;
}

public void OnEntityCreated(int entity, const char[] classname)
{
	if (entity <= MaxClients || !IsValidEntity(entity))
		return;

	if (classname[0] != 'w')
		return;

	if (strcmp(classname, "witch") == 0)
		SDKHook(entity, SDKHook_OnTakeDamage, WitchOnTakeDamage);
}

public Action WitchOnTakeDamage(int victim, int &attacker, int &inflictor, float &damage, int &damagetype, int &weapon, float damageForce[3], float damagePosition[3])
{
	if (!IsValidClient(attacker) || !IsValidWitch(victim))
	{
		return Plugin_Continue;
	}


	weapon = GetEntPropEnt(attacker, Prop_Send, "m_hActiveWeapon");
	if (weapon == -1 || !IsValidEntity(weapon))
	{
		return Plugin_Continue;
	}

	char sName[32]; 
	GetEntityClassname(weapon, sName, sizeof(sName));

	if (strcmp(sName, "weapon_grenade_launcher") == 0)
	{
		SDKHooks_TakeDamage(victim, inflictor, attacker, damage, DMG_BLAST);		
		return Plugin_Changed;  
	}

	return Plugin_Continue;
}

void Event_PlayerHurt(Event event, const char[] name, bool dontBroadcast)
{
	int attacker = GetClientOfUserId(event.GetInt("attacker"));
	int victim = GetClientOfUserId(event.GetInt("userid"));
    
	// 1. 检查 attacker 是否是有效玩家
	if (!IsValidClient(attacker))
	{
		return;
	}
    
	// 2. 获取武器并检查有效性
	int weapon = GetEntPropEnt(attacker, Prop_Send, "m_hActiveWeapon");
	if (!IsValidEntity(weapon))
	{
		return;
	}
    
	// 3. 检查武器类型
	char classname[32];
	GetEntityClassname(weapon, classname, sizeof(classname));
	if (strcmp(classname, "weapon_grenade_launcher") == 0)
	{
		// 4. 检查 victim 是否是特殊感染者
		if (IsValidInfected(victim))
		{
			L4D_StaggerPlayer(victim, attacker, NULL_VECTOR);
		}
	}
}
